package ch.ethz.fcl.googlemap;

import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import ch.ethz.fcl.R;
import ch.ethz.fcl.draw.RangeSeekBar;
import ch.ethz.fcl.listener.OrientationListener;
import ch.ethz.fcl.listener.SetButtonClickListener;
import ch.ethz.fcl.listener.TimerListener;
import ch.ethz.fcl.overlays.CircleOverlay;
import ch.ethz.fcl.overlays.OverlayManager;
import ch.ethz.fcl.overlays.ScreenLineOverlay;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class MetroBuzzGoogleMaps extends MapActivity {
	public MapView mapView = null;
	public static GeoPoint geoPoint1 = null;
	public Paint paint;
	public static View main_view;
	private OrientationListener ol;
	public CircleOverlay circle1;
	public CircleOverlay circle2;
	public ScreenLineOverlay screenLine1;
	public ScreenLineOverlay screenLine2;
	public LinearLayout layout_timer1;
	public LinearLayout layout_timer2;
	public RangeSeekBar<Integer> timer1;
	public RangeSeekBar<Integer> timer2;

	private boolean initialized = false;

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_main2);

		main_view = getWindow().getDecorView();
		main_view.setSystemUiVisibility(View.STATUS_BAR_HIDDEN);

		initOverlays();
		registerButtons();
		ol = new OrientationListener(this);
		ol.init();
		initTimer();

	}

	public void onPause() {
		super.onPause();
		if (initialized)
			ol.onPause();
	}

	public void registerButtons() {
		ImageButton image_button_network = (ImageButton) findViewById(R.id.imageButton_setup);
		image_button_network
				.setOnClickListener(new SetButtonClickListener(this));

		ImageButton image_button_setting = (ImageButton) findViewById(R.id.imageButton_reset);
		image_button_setting
				.setOnClickListener(new SetButtonClickListener(this));

		// ImageButton[] buttons = new ImageButton[4];
		// buttons[0] = (ImageButton) MetroBuzzGoogleMaps.main_view
		// .findViewById(R.id.imageButton_one_circle);
		// buttons[1] = (ImageButton) MetroBuzzGoogleMaps.main_view
		// .findViewById(R.id.imageButton_two_circle);
		// buttons[2] = (ImageButton) MetroBuzzGoogleMaps.main_view
		// .findViewById(R.id.imageButton_one_line);
		// buttons[3] = (ImageButton) MetroBuzzGoogleMaps.main_view
		// .findViewById(R.id.imageButton_two_line);
		//
		// for (int i = 0; i < buttons.length; i++) {
		// buttons[i].setBackgroundColor(Color.GRAY);
		// if (i == 0)
		// buttons[i].setBackgroundColor(Color.GREEN);
		// buttons[i].setOnClickListener(new ModeButtonClickListener(
		// buttons[i], mapView));
		// }
	}

	public void initOverlays() {
		circle1 = new CircleOverlay(this, 1);
		circle2 = new CircleOverlay(this, 2);
		// screenLine1 = new ScreenLineOverlay(this, 1);
		// screenLine2 = new ScreenLineOverlay(this, 2);

		mapView = (MapView) findViewById(R.id.mapview);
		// mapView.setTileSource(new OnlineTileSourceBase("GoogleMaps",
		// ResourceProxy.string.unknown, 1, 20, 256, ".png",
		// "http://mt3.google.com/vt/v=w2.97") {
		// @Override
		// public String getTileURLString(final MapTile aTile) {
		// /*
		// * GOOGLE MAPS URL looks like base url const x y zoom
		// * http://mt3.google.com/vt/v=w2.97&x=74327&y=50500&z=17
		// */
		// Log.d(TAG,
		// getBaseUrl() + "&x=" + aTile.getX() + "&y="
		// + aTile.getY() + "&z=" + aTile.getZoomLevel());
		// return getBaseUrl() + "&x=" + aTile.getX() + "&y="
		// + aTile.getY() + "&z=" + aTile.getZoomLevel();
		// }
		// });

		// mapView.setUseDataConnection(true);
		// mapView.setMultiTouchControls(true);

		OverlayManager om = new OverlayManager(mapView);

		// signapore
		geoPoint1 = new GeoPoint(1367668, 103812561);

		GeoPoint geoPoint2 = getGeo(1367668, 103779602);
		GeoPoint geoPoint3 = getGeo(1245701, 103881226);
		GeoPoint geoPoint4 = getGeo(1340210, 103919678);

		// zurich
		// geoPoint1 = new GeoPoint(47366330, 8542108);
		// GeoPoint geoPoint2 = getGeo(47303330, 8585108);
		// GeoPoint geoPoint3 = getGeo(47403000, 8400000);
		// GeoPoint geoPoint4 = getGeo(47500000, 8400000);
		mapView.getController().setZoom(12);

		circle1.init(geoPoint2, geoPoint1);
		circle2.init(geoPoint3, geoPoint4);
		// screenLine1.init(geoPoint1, geoPoint2);
		// screenLine2.init(geoPoint3, geoPoint2);

		if (om.size() == 0) {
			om.addList(circle2);
			om.addList(circle1);
			om.addList(screenLine1);
			om.addList(screenLine2);
		}

		circle1.sendInfo();
		circle1.sendFingerUp();
		circle2.sendInfo();
		circle2.sendFingerUp();

		mapView.getOverlays().add(circle1);
		mapView.getOverlays().add(circle2);
	}

	public void initTimer() {
		layout_timer1 = (LinearLayout) findViewById(R.id.linearLayout4);
		layout_timer2 = (LinearLayout) findViewById(R.id.linearLayout5);

		timer1 = new RangeSeekBar<Integer>(1, 96, this);
		TimerListener tl1 = new TimerListener("Timer1");
		timer1.setOnRangeSeekBarChangeListener(tl1);
		tl1.rangeSeekBarValuesChanged(1, 96);

		timer2 = new RangeSeekBar<Integer>(1, 96, this);
		TimerListener tl2 = new TimerListener("Timer2");
		timer2.setOnRangeSeekBarChangeListener(tl2);
		tl2.rangeSeekBarValuesChanged(1, 96);

		layout_timer1.addView(timer1);
		layout_timer2.addView(timer2);
		initialized = true;
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		if (initialized) {
			timer1.setPadding(layout_timer1.getWidth() / 2);
			mapView.getController().setCenter(geoPoint1);
			timer2.setPadding(layout_timer2.getWidth() / 2);
		}
	}

	private GeoPoint getGeo(int lat, int lon) {
		return new GeoPoint(lat, lon);
	}
}
